#pragma once
#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>

#include<assert.h>
#include< initializer_list >


namespace gyc {

	template<class T>
	class vector {
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}

		vector()
		{
		}
		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (size_t i; i < n; i++)
			{
				push_back(val);
			}
		}
		vector(int n, const T& val = T())
		{
			reserve(n);
			for (size_t i; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(std::initializer_list<T> il)
		{
			reserve(il.size());
			for(auto & e: il)
			{
				push_back(e);
			}
		}

		~vector()
		{
			if (_start)
			{
				delete[]_start;
				_start = _finish = _end_of_storage = nullptr;
			}

		}

		//拷贝构造
		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto& e : v)
			{
				push_back(e);
			}

		}

	   //实现自己的swap
		void swap(const vector<T> v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}


  		//赋值重载  v1 = v3;
		vector<T>& operator=(vector<T> v) const
		{
			swap(v);
			return *this;
		}


		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		size_t size()
		{
			return _finish - _start;
		}

		T& operator[](size_t i)
		{
			assert(i < size());
			return _start[i];
		}

		void reserve(size_t n)
		{
			size_t old_size = size();
			if (n > capacity())
			{
				T* tmp = new T[n];
				if (_start)
				{
					//memcpy(tmp, _start, sizeof(T) * old_size);
					for (size_t i = 0; i < old_size; i++)
					{
						tmp[i] = _start[i];
					}
	                delete[]_start;
				}
				_start = tmp;
				_finish = _start + old_size;
				_end_of_storage = _start + n;

			}
		}

		void resize(size_t n,T val = T())
		{
			if (n > size())
			{
				reserve(n);
				while (_finish != _start + n)
				{
					*_finish = val;
					_finish++;
				}
			}
			else {

				_finish = _start + n;
			}
		}


		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
			}
			*_finish = x;
			_finish++;
		}


		void pop_back()
		{
			assert(_finish > _start);
			 --_finish;
		}


		void insert(iterator pos,const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish); 
			
			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
				pos = _start + len;
				
			}
			iterator it = _finish - 1;

			while (pos <= it)
			{
				*(it + 1) = *it;
				it--;
			}

			*pos = x;
			_finish++;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);

			iterator it = pos + 1;

			while (it < _finish)
			{
				*(it - 1) = *it;
				it++;
			}

			_finish--;

			return pos;
		}

		template<class Inputiterator>
		vector(Inputiterator first, Inputiterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}



	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;

	};




}